//package com.ruoyi.wechat.interceptor;
//
//import com.alibaba.fastjson.JSON;
//import com.ruoyi.common.constant.HttpStatus;
//import com.ruoyi.common.core.domain.AjaxResult;
//import com.ruoyi.common.utils.ServletUtils;
//import com.ruoyi.common.utils.StringUtils;
//import com.ruoyi.wechat.component.WeChatTokenService;
//import com.ruoyi.wechat.mapper.WeChatLoginUser;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.web.servlet.HandlerInterceptor;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.util.Arrays;
//
///**
// * @author cj
// */
//@Component
//public class WeChatTokenInterceptor implements HandlerInterceptor {
//
//
//    @Autowired
//    WeChatTokenService weChatTokenService;
//
//    /**
//     * 白名单
//     */
//    private final String[] whitelist = {"/base/login","/system/captchaImage","/hello","/base/redirect/auth"};
//
//
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
//            throws Exception {
//        String requestUrl = request.getRequestURI().split("/wechat")[1];
//        if (Arrays.asList(whitelist).contains(requestUrl)){
//            return true;
//        }
//        WeChatLoginUser weChatLoginUser = weChatTokenService.getLoginUser(request);
//        if (weChatLoginUser != null){
//            return true;
//        }
//        //认证失败
//        int code = HttpStatus.UNAUTHORIZED;
//        String msg = StringUtils.format("请求访问：{}，认证失败，无法访问系统资源", request.getRequestURI());
//        ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(code, msg)));
//        //返回 false 则请求中断
//        return false;
//    }
//
//}
